﻿// CIOOutputDlg.cpp: 实现文件
//

#include "pch.h"
#include "SP.h"
#include "CIOOutputDlg.h"
#include "afxdialogex.h"


// CIOOutputDlg 对话框

IMPLEMENT_DYNAMIC(CIOOutputDlg, CDialogEx)

CIOOutputDlg::CIOOutputDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DLG_IO_OUTPUT, pParent)
{

}

CIOOutputDlg::~CIOOutputDlg()
{
}

void CIOOutputDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BOOL CIOOutputDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	SetTimer(TIMER_ID_IO_CONTROL_STATUS_OUT, 50, NULL);
	ChangeIoTextOut();
	SetIOStatus();
	return TRUE;
}

void CIOOutputDlg::OnCancel()
{
	KillTimer(TIMER_ID_IO_CONTROL_STATUS_OUT);
	CDialogEx::OnCancel();
}


BEGIN_MESSAGE_MAP(CIOOutputDlg, CDialogEx)
	ON_WM_TIMER()
	ON_CONTROL_RANGE(BN_CLICKED, IDC_IY0, IDC_IY31, &CIOOutputDlg::OnBnClickedOption)
END_MESSAGE_MAP()


// CIOOutputDlg 消息处理程序


void CIOOutputDlg::ChangeIoTextOut()
{
	std::vector<IO_MODULE> vIoMap;
	vIoMap = *Config.GetIOOutputSetting();
	for (int i = 0; i < 18; i++)
	{
		CWnd * pStatic = nullptr;

		pStatic = (CWnd*)GetDlgItem(IDC_STATIC_TIY0 + i);//控件编号问题
		CString cstr;
		cstr.Format(L"%d_", i);
		cstr.Append(vIoMap[i].m_cstrDescription);
		pStatic->SetWindowTextW(cstr);

	}
}

void CIOOutputDlg::SetIOStatus()
{
	CButton* pCheck = nullptr;
	pCheck = (CButton*)GetDlgItem(IDC_IY0);  pCheck->SetCheck(Manager.GetCio()->Y00);
	pCheck = (CButton*)GetDlgItem(IDC_IY1);  pCheck->SetCheck(Manager.GetCio()->Y01);
	pCheck = (CButton*)GetDlgItem(IDC_IY2);  pCheck->SetCheck(Manager.GetCio()->Y02);
	pCheck = (CButton*)GetDlgItem(IDC_IY3);  pCheck->SetCheck(Manager.GetCio()->Y03);
	pCheck = (CButton*)GetDlgItem(IDC_IY4);  pCheck->SetCheck(Manager.GetCio()->Y04);
	pCheck = (CButton*)GetDlgItem(IDC_IY5);  pCheck->SetCheck(Manager.GetCio()->Y05);
	pCheck = (CButton*)GetDlgItem(IDC_IY6);  pCheck->SetCheck(Manager.GetCio()->Y06);
	pCheck = (CButton*)GetDlgItem(IDC_IY7);  pCheck->SetCheck(Manager.GetCio()->Y07);
	pCheck = (CButton*)GetDlgItem(IDC_IY8);  pCheck->SetCheck(Manager.GetCio()->Y08);
	pCheck = (CButton*)GetDlgItem(IDC_IY9);  pCheck->SetCheck(Manager.GetCio()->Y09);
	pCheck = (CButton*)GetDlgItem(IDC_IY10);  pCheck->SetCheck(Manager.GetCio()->Y10);
	pCheck = (CButton*)GetDlgItem(IDC_IY11);  pCheck->SetCheck(Manager.GetCio()->Y11);
	pCheck = (CButton*)GetDlgItem(IDC_IY12);  pCheck->SetCheck(Manager.GetCio()->Y12);
	pCheck = (CButton*)GetDlgItem(IDC_IY13);  pCheck->SetCheck(Manager.GetCio()->Y13);
	pCheck = (CButton*)GetDlgItem(IDC_IY14);  pCheck->SetCheck(Manager.GetCio()->Y14);
	pCheck = (CButton*)GetDlgItem(IDC_IY15);  pCheck->SetCheck(Manager.GetCio()->Y15);
	pCheck = (CButton*)GetDlgItem(IDC_IY16);  pCheck->SetCheck(Manager.GetCio()->Y16);
	pCheck = (CButton*)GetDlgItem(IDC_IY17);  pCheck->SetCheck(Manager.GetCio()->Y17);
}

void CIOOutputDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if (nIDEvent == TIMER_ID_IO_CONTROL_STATUS_OUT)
	{
		SetIOStatus();
	}

	CDialogEx::OnTimer(nIDEvent);
}

void CIOOutputDlg::OnBnClickedOption(UINT nID)
{
	int nPort = nID - IDC_IY0;
	//nPort = nPort / 2;
	CString cstr;
	cstr.Format(L"%d", nPort);

	CButton* pCheckBox = (CButton*)GetDlgItem(nID);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		Manager.GetCio()->SetOutPutIO(nPort, ON);
		cstr.Append(L"选中");
		Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
	}
	if (nFlag == BST_UNCHECKED)
	{
		Manager.GetCio()->SetOutPutIO(nPort, OFF);
		cstr.Append(L"取消");
		Manager.GetLog()->DisplayAndSaveLog(cstr.GetBuffer(), LV_EVENT);
	}

}
